<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // const url = new URL("https://www.example.com/path?key1=value1&key2=value2");

        // // 获取各个部分
        // const protocol = url.protocol; // "https:"`
        // const hostname = url.hostname; // "www.example.com"
        // const pathname = url.pathname; // "/path"
        // const searchParams = url.searchParams; // URLSearchParams对象
        // console.log(searchParams)

        // const search = url.search; // "?key1=value1&key2=value2"

        // // 获取查询参数
        // const key1 = searchParams.get("key1"); // "value1"
        // const key2 = searchParams.get("key2"); // "value2"



        const url = "https://www.example.com/path?key1=value1&key2=value2";

        const result = /^(https?:\/\/)?([^/?#]+)(\/[^?#]*)?(\?[^#]*)?(#.*)?$/.exec(url);
        const protocol = result[1]; // "https://"
        const hostname = result[2]; // "www.example.com"
        const pathname = result[3]; // "/path"
        const search = result[4]; // "?key1=value1&key2=value2"

        // 解析查询参数
        const searchParams = new URLSearchParams(search.slice(1));
        const key1 = searchParams.get("key1"); // "value1"
        const key2 = searchParams.get("key2"); // "value2"
    </script>
</body>

</html>